package com.example.videoworks.controller;


import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.videoworks.common.CommonException;
import com.example.videoworks.common.Result;
import com.example.videoworks.controller.request.GetListRequest;
import com.example.videoworks.entity.Work;
import com.example.videoworks.mapper.WorkMapper;
import com.example.videoworks.service.IWorkService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;
import java.util.Map;

import static ch.qos.logback.core.util.StatusPrinter.print;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Lily
 * @since 2023-05-11
 */
@RestController
@RequestMapping("/work")
public class WorkController {
    @Autowired
    private IWorkService workService;

    @GetMapping(value="/id",produces = "application/json")
    @ApiOperation(value = "同一作品不同版本详情")
    @ApiImplicitParam(name = "workId",value = "作品id",required = true,paramType = "query",dataType = "String")
    public Result getWorkById(@NotNull @Valid @RequestBody GetListRequest request, @NotNull @RequestParam("workId")String id){
        try{
            return Result.success(workService.getWorkById(request, id));
        }catch (CommonException e){
            return Result.result(e.getCommonErrorCode());
        }
    }
    @GetMapping(value="/get",produces = "application/json")
    @ApiOperation(value = "某一作品详情")
    @ApiImplicitParams({@ApiImplicitParam( name = "workId",value = "作品id",required = true,paramType = "query",dataType = "String"),
            @ApiImplicitParam( name = "ordering",value = "作品版本号",required = false,paramType = "query",dataType = "Integer")})
    public Result getWork(@NotNull @RequestParam("workId")String id, @RequestParam("ordering")Integer ordering){
        try{
            return Result.success(workService.getWork(id, ordering));
        }catch (CommonException e){
            return Result.result(e.getCommonErrorCode());
        }
    }

    @GetMapping(value="/searchtitle",produces = "application/json")
    @ApiOperation(value = "通过名称模糊查询作品")
    @ApiImplicitParam(name = "title",value = "作品名称",required = true,paramType = "query",dataType = "String")
    public Result findWorkByTitle(@NotNull @Valid @RequestBody GetListRequest request, @NotNull @RequestParam("title")String title) {
        try{
            return Result.success(workService.findWorkByTitle(request, title));
        }catch (CommonException e){
            return Result.result(e.getCommonErrorCode());
        }
    }

    @GetMapping(value="/ratework",produces = "application/json")
    @ApiOperation(value = "通过评分排序同类型作品")
    @ApiImplicitParam(name = "type",value = "作品类型",required = true,paramType = "query",dataType = "String")
    public Result rateOneTypeWork(@NotNull @Valid @RequestBody GetListRequest request, @NotNull @RequestParam("type")String type) {
        try{
            return Result.success(workService.rateOneTypeWork(request, type));
        }catch (CommonException e){
            return Result.result(e.getCommonErrorCode());
        }
    }

    @GetMapping(value="/ratework2",produces = "application/json")
    @ApiOperation(value = "通过上映年份排序同类型作品")
    @ApiImplicitParam(name = "type",value = "作品类型",required = true,paramType = "query",dataType = "String")
    public Result rateOneTypeWorkByStartyear(@NotNull @Valid @RequestBody GetListRequest request, @NotNull @RequestParam("type")String type) {
        try{
            return Result.success(workService.rateOneTypeWorkByStartyear(request, type));
        }catch (CommonException e){
            return Result.result(e.getCommonErrorCode());
        }
    }

    @GetMapping(value="/searchontype",produces = "application/json")
    @ApiOperation(value = "对某一类型的影视作品做模糊查询")
    @ApiImplicitParams({@ApiImplicitParam( name = "searchParam",value = "查询关键词",required = true,paramType = "query",dataType = "String"),
            @ApiImplicitParam( name = "type",value = "作品类型",required = true,paramType = "query",dataType = "String")})
    public Result searchOnType(@NotNull @Valid @RequestBody GetListRequest request,
                               @NotNull @RequestParam("searchParam")String searchParam, @RequestParam("type")String type){
        try{
            return Result.success(workService.searchOnType(request, searchParam, type));
        }catch (CommonException e){
            return Result.result(e.getCommonErrorCode());
        }
    }

    @GetMapping(value="/filtertype", produces = "application/json")
    @ApiOperation(value = "通过类型过滤作品")
    @ApiImplicitParam(name = "type",value = "作品类型",required = true,paramType = "query",dataType = "String")
    public Result filterType(@NotNull @Valid @RequestBody GetListRequest request, @NotNull @RequestParam("type")String type) {
        try{
            return Result.success(workService.filterType(request, type));
        }catch (CommonException e){
            return Result.result(e.getCommonErrorCode());
        }
    }

    @GetMapping(value="/filtergenre",produces = "application/json")
    @ApiOperation(value = "通过标签过滤作品")
    @ApiImplicitParam(name = "genre",value = "作品标签",required = true,paramType = "query",dataType = "String")
    public Result filterGenre(@NotNull @Valid @RequestBody GetListRequest request, @NotNull @RequestParam("genre")String genre) {
        try{
            return Result.success(workService.filterGenre(request, genre));
        }catch (CommonException e){
            return Result.result(e.getCommonErrorCode());
        }
    }

    @GetMapping(value="/filterregion",produces = "application/json")
    @ApiImplicitParam(name = "region",value = "作品地区",required = true,paramType = "query",dataType = "String")
    public Result filterRegion(@NotNull @Valid @RequestBody GetListRequest request, @NotNull @RequestParam("region")String region) {
        try{
            return Result.success(workService.filterRegion(request, region));
        }catch (CommonException e){
            return Result.result(e.getCommonErrorCode());
        }
    }

    @GetMapping(value="/advancesearch",produces = "application/json")
    @ApiOperation(value = "对电影做高级搜索")
    @ApiImplicitParams({@ApiImplicitParam( name = "searchParam",value = "查询关键词",required = true,paramType = "query",dataType = "String"),
            @ApiImplicitParam( name = "lowerYear",value = "最小上映年份",required = true,paramType = "query",dataType = "String"),
            @ApiImplicitParam( name = "upperYear",value = "最大上映年份",required = true,paramType = "query",dataType = "String"),
            @ApiImplicitParam( name = "type",value = "作品类型",required = true,paramType = "query",dataType = "String"),
            @ApiImplicitParam( name = "region",value = "作品地区",required = true,paramType = "query",dataType = "String"),
            @ApiImplicitParam( name = "genre",value = "作品标签",required = true,paramType = "query",dataType = "String")
    })
    public Result searchOnType(@NotNull @Valid @RequestBody GetListRequest request,
                               @NotNull @RequestParam("searchParam")String searchParam, String lowerYear, String upperYear, String type, String region, String genre){
        try{
            return Result.success(workService.advancedSearch(request, searchParam, lowerYear, upperYear, type, region, genre));
        }catch (CommonException e){
            return Result.result(e.getCommonErrorCode());
        }
    }

    @GetMapping(value="/searchorderrating",produces = "application/json")
    @ApiOperation(value = "对电影做高级搜索并按评分排序")
    @ApiImplicitParams({@ApiImplicitParam( name = "searchParam",value = "查询关键词",required = true,paramType = "query",dataType = "String"),
            @ApiImplicitParam( name = "lowerYear",value = "最小上映年份",required = true,paramType = "query",dataType = "String"),
            @ApiImplicitParam( name = "upperYear",value = "最大上映年份",required = true,paramType = "query",dataType = "String"),
            @ApiImplicitParam( name = "type",value = "作品类型",required = true,paramType = "query",dataType = "String"),
            @ApiImplicitParam( name = "region",value = "作品地区",required = true,paramType = "query",dataType = "String"),
            @ApiImplicitParam( name = "genre",value = "作品标签",required = true,paramType = "query",dataType = "String")
    })
    public Result SearchOrderRating(@NotNull @Valid @RequestBody GetListRequest request,
                               @NotNull @RequestParam("searchParam")String searchParam, String lowerYear, String upperYear, String type, String region, String genre){
        try{
            return Result.success(workService.SearchOrderRating(request, searchParam, lowerYear, upperYear, type, region, genre));
        }catch (CommonException e){
            return Result.result(e.getCommonErrorCode());
        }
    }

    @GetMapping(value="/searchorderyear",produces = "application/json")
    @ApiOperation(value = "对电影做高级搜索并按上映年份排序")
    @ApiImplicitParams({@ApiImplicitParam( name = "searchParam",value = "查询关键词",required = true,paramType = "query",dataType = "String"),
            @ApiImplicitParam( name = "lowerYear",value = "最小上映年份",required = true,paramType = "query",dataType = "String"),
            @ApiImplicitParam( name = "upperYear",value = "最大上映年份",required = true,paramType = "query",dataType = "String"),
            @ApiImplicitParam( name = "type",value = "作品类型",required = true,paramType = "query",dataType = "String"),
            @ApiImplicitParam( name = "region",value = "作品地区",required = true,paramType = "query",dataType = "String"),
            @ApiImplicitParam( name = "genre",value = "作品标签",required = true,paramType = "query",dataType = "String")
    })
    public Result SearchOrderYear(@NotNull @Valid @RequestBody GetListRequest request,
                                    @NotNull @RequestParam("searchParam")String searchParam, String lowerYear, String upperYear, String type, String region, String genre){
        try{
            return Result.success(workService.SearchOrderYear(request, searchParam, lowerYear, upperYear, type, region, genre));
        }catch (CommonException e){
            return Result.result(e.getCommonErrorCode());
        }
    }
}



